package bakenote.xml;

import bakenote.ui.BakeNoteWindow;
import bakenote.core.Recipe;
import bakenote.data.RecipeSQLiteDB;
import bakenote.data.RecipeList;
import org.apache.pivot.collections.List;
import org.apache.pivot.io.IOTask;
import org.apache.pivot.util.concurrent.TaskExecutionException;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ImportXMLTask extends IOTask<Void> {

        private File xmlFile;
        private Recipe recipe;

        public ImportXMLTask(File xmlFile) {
            this.xmlFile = xmlFile;
        }

        public Recipe getRecipe() {
            return recipe;
        }

        @Override
        public Void execute() throws TaskExecutionException {

            try {
                List<Recipe> rl = RecipeXMLReader.parseXML(xmlFile);
                for (Recipe r : rl) {
                    // save recipe r to DB
                    RecipeSQLiteDB.saveRecipe(r);
                    // add the recipe to the List
                    RecipeList.add(r);
                    recipe = r;
                }
            } catch (Exception ex) {
                Logger.getLogger(BakeNoteWindow.class.getName()).log(Level.SEVERE, null, ex);
            }

            return null;

        }
}
